<?php

namespace StudyBuddy\Event;

class Notification extends Dispatcher {
    /**
     * Default state of the notification
     */
    const DEFAULT_STATE = 0;


    /**
     * Notification has been cancelled
     */
    const CANCELLED_STATE = 1;


    /**
     * name of the notofication
     * @var string
     */
    protected $notificationName;

    /**
     * object of interest
     * (the sender of the notification, in most cases)
     * @var object
     */
    protected $notificationObject;

    /**
     * additional information about the notification
     * @var mixed
     */
    protected $notificationInfo = array();

    /**
     * state of the notification
     *
     * This may be:
     * - self::DEFAULT_STATE
     * - self::CANCELLED_STATE
     *
     * @var integer
     */
    protected $notificationState = self::DEFAULT_STATE;

    /**
     * amount of observers that received this notification
     * @var mixed
     */
    protected $notificationCount = 0;

    /**
     * Constructor
     *
     * @param object $object The object of interest for the notification,
     *                       usually is the posting object
     * @param string $name   Notification name
     * @param array  $info   Free information array
     *
     * @return void
     */
    public function __construct($object, $name, array $info = array()) {
        $this->notificationObject = $object;
        $this->notificationName = $name;
        $this->notificationInfo = $info;
    }

    /**
     * For information purposed and for purposes on loggin, so
     * you may add the class to log like $log($oEvent)
     * where $oEvent is this object
     *
     * @return string with info about this object
     */
    public function __toString() {
        return 'Notification object. "Event notification name":' . $this->notificationName;
    }

    /**
     * Returns the notification name
     *
     * @return  string Notification name
     */
    function getNotificationName() {
        return $this->notificationName;
    }

    /**
     * Returns the contained object
     *
     * @return  object Contained object
     */
    public function getNotificationObject() {
        return $this->notificationObject;
    }

    /**
     * Returns the user info array
     *
     * @return  array user info
     */
    public function getNotificationInfo() {
        return $this->notificationInfo;
    }

    /**
     * Increase the internal count
     *
     * @access   public
     * @return void
     */
    public function increaseNotificationCount() {
        ++$this->notificationCount;
    }

    /**
     * Get the number of posted notifications
     * This info is usefull for testing and debuging
     *
     * @return   int
     */
    public function getNotificationCount() {
        return $this->notificationCount;
    }

    /**
     * Cancel the notification
     *
     * @return   void
     */
    public function cancelNotification() {
        $this->notificationState = self::CANCELLED_STATE;
    }

    /**
     * Checks whether the notification has been cancelled
     *
     * @return   boolean
     */
    public function isNotificationCancelled() {
        return ($this->notificationState === self::CANCELLED_STATE);
    }

}
